/**
 *仪表实时数据
 */
'use strict';
const HGetAllMultiData = require("../../libs/Redis/HGetAllMultiData");
const _ = require("underscore");

module.exports = async function (params) {
    let keyList = [];
    let deviceInfoList = params.deviceInfoList;
    for(const device of deviceInfoList){
        keyList.push(`realTime-${params.type}-${device.deviceNo}`)
    }
    let deviceDataMultiList = await HGetAllMultiData({
        keyList:keyList,
    })
    let realTimeDataList = [];
    deviceDataMultiList = _.filter(deviceDataMultiList,function (deviceData){
        return deviceData
    })
    for(const deviceInfo of deviceInfoList){
        let deviceRealData = _.find(deviceDataMultiList,function (data){
            return data.device_no === deviceInfo.deviceNo;
        })
        if(deviceRealData){
            for(let column in deviceRealData){
                let columnList = column.split("_");
                let keyName = columnList[0];
                for(let index = 1 ;index <columnList.length;index++){
                    keyName += (columnList[index].charAt(0).toUpperCase() + columnList[index].slice(1));
                }
                deviceInfo[keyName] = deviceRealData[column]
            }
        }
        realTimeDataList.push(deviceInfo);
    }
    let dataGroup = _.groupBy(realTimeDataList,"parentId");
    let dataParentObj = {};
    let dataChildObj = {};
    for(const parentId in dataGroup){
        let deviceDataList = dataGroup[parentId];
        if(parentId === "0"){
            for(const parent of deviceDataList) {
                dataParentObj[`${parent.deviceId}`] = parent;
            }
        }else{
            for(const children of deviceDataList){
                if(dataChildObj[`${children.parentId}`]){
                    dataChildObj[`${children.parentId}`].push(children)
                }else{
                    dataChildObj[`${children.parentId}`] = [children];
                }
            }
        }
    }
    let dataList = [];
    for(const parentId in dataParentObj){
        if(dataChildObj[`${parentId}`]){
            dataParentObj[`${parentId}`].children = dataChildObj[`${parentId}`];
        }
        dataList.push(dataParentObj[`${parentId}`]);
    }
    return { success: true,dataList:dataList};
};